package com.arvinclub.web.controller.fun;

import com.arvinclub.model.entity.Blog;
import com.arvinclub.model.entity.User;
import com.arvinclub.service.service.BlogService;
import com.arvinclub.service.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 个人主页
 */
@Controller
public class PageController {
    @Resource
    private BlogService blogService;

    @Resource
    private UserService userService;

    /**
     * 进入我的个人主页（默认第一页）
     */
    @GetMapping("user/page.html")
    public String seeMe(Model model, HttpSession session) {
        return seeWithPage(0, model, "1", session);
    }

    /**
     * 进入某人个人主页（默认第一页）
     */
    @GetMapping("user/page.html/{userID}")
    public String see(@PathVariable int userID, Model model, HttpSession session) {
        return seeWithPage(userID, model, "1", session);
    }

    /**
     * 进入某人个人主页（指定页号）
     */
    @GetMapping("user/page.html/{userID}/{str}")
    public String seeWithPage(@PathVariable int userID, Model model, @PathVariable String str, HttpSession session) {
        /* 确认用户*/
        User me = (User) session.getAttribute("user");
        User someone = userService.findUserById(userID == 0 ? me.getId() : userID);
        /*确认页号,开始查询*/
        PageInfo<Blog> blogPage = blogService.selectBlogsByUserId(someone.getId(), Integer.parseInt(str));
        /*返回信息*/
        model.addAttribute("blogPage", blogPage);
        model.addAttribute("someone", someone);
        boolean hoster = me.equals(someone);
        model.addAttribute("hoster", hoster);
        if (!hoster)
            model.addAttribute("isAttention", userService.isAttention(me.getId(), someone.getId()));
        return "page";
    }
}
